package com.z6school.manage.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.z6school.manage.common.annotation.RequiredLog;
import com.z6school.manage.common.exception.ServiceException;
import com.z6school.manage.common.pojo.CheckBox;
import com.z6school.manage.common.pojo.PageObject;
import com.z6school.manage.common.pojo.RoleMenuVo;
import com.z6school.manage.mapper.RolesMapper;
import com.z6school.manage.mapper.RolesMenusMapper;
import com.z6school.manage.mapper.UserRolesMapper;
import com.z6school.manage.po.Role;
import com.z6school.manage.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private RolesMenusMapper rolesMenusMapper;
    @Autowired
    private UserRolesMapper userRolesMapper;

    @Override
    @RequiredLog(operation = "查询角色信息")
    @RequiresPermissions("sys:role:view")
    public PageObject<Role> doFindObject(String name, int pageCurrent) {
        long rowCount = rolesMapper.getRowCount(name);
        if (rowCount == 0) throw new ServiceException("记录不存在");
        int pageSize = 5;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<Role> roles = rolesMapper.doFindObject(name, startIndex, pageSize);
        return new PageObject<Role>(rowCount, roles, pageSize, pageCurrent);
    }

    @Override
    @RequiredLog(operation = "删除角色信息")
    //@RequiresPermissions("sys:role:delete")
    public int deleteObject(Integer id) {
        if (id == null || id <= 0)
            throw new IllegalArgumentException("请先选择");
        rolesMenusMapper.deleteObjectByRoleId(id);
        userRolesMapper.deleteObjectByRoleId(id);
        int rows = rolesMapper.deleteById(id);
        if (rows == 0)
            throw new ServiceException("此纪录可能已经不存在");
        return rows;
    }

    @Override
    @RequiredLog(operation = "新增角色信息")
    //@RequiresPermissions("sys:role:add")
    public int saveObject(Role entity, Integer[] menusId) {
        if (entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if (StringUtil.isEmpty(entity.getName()))
            throw new IllegalArgumentException("角色名不能为空");
        if (menusId == null || menusId.length == 0)
            throw new IllegalArgumentException("需要为角色指定权限");
        int rows = rolesMapper.insert(entity);
        if (rows == 0)
            throw new ServiceException("该角色可能已存在");
        entity.setCreatedTime(new Date()).setModifiedTime(new Date());
        rolesMenusMapper.insertObjects(entity.getId(), menusId);
        return rows;
    }

    @Override
    @RequiredLog(operation = "查询角色菜单关系信息")
    public RoleMenuVo findRoleMenuById(Integer id) {
        if (id == null || id <= 0)
            throw new IllegalArgumentException("id值不合法");
        RoleMenuVo result = rolesMapper.findRoleMenuById(id);
        if (result == null)
            throw new ServiceException("此纪录已经不存在");
        return result;
    }

    @Override
    @RequiredLog(operation = "更新角色信息")
    //@RequiresPermissions("sys:role:update")
    public int updateObject(Role role, Integer[] menuIds) {

        if ((role == null))
            throw new IllegalArgumentException("更新的对象不能为空");
        if (role.getId() == null)
            throw new IllegalArgumentException("id的值不能为空");
        if (StringUtil.isEmpty(role.getName()))
            throw new IllegalArgumentException("角色名不能为空");
        if (menuIds == null || menuIds.length == 0)
            throw new IllegalArgumentException("必须为角色指定一个指定权限");

        role.setModifiedTime(new Date());
        int rows = rolesMapper.updateById(role);
        if (rows==0)
            throw new ServiceException("对象可能已经不存在");

        rolesMenusMapper.deleteObjectByRoleId(role.getId());
        rolesMenusMapper.insertObjects(role.getId(), menuIds);
        return rows;
    }

    /**查询角色id，name相关信息*/
    @Override
    @RequiredLog(operation = "查询角色信息")
    public List<CheckBox> findCheckBoxObjects() {
        return rolesMapper.findCheckBoxObjects();
    }
}
